package com.rongshu.view.penpal.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;

import com.rongshu.R;
import com.rongshu.view.base.BaseFragment;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.penpal.adapter.PenpalListAdapter;
import com.rongshu.view.penpal.bean.PenpalResponse;
import com.rongshu.view.penpal.present.PenpalJoinedPresenter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * created by zhengweis on 2018/11/5.
 * description：笔友会-我的
 */
public class Penpal_JoinedFragment extends BaseFragment<PenpalJoinedPresenter> implements INetView {
    @BindView(R.id.refresh_view)
    SmartRefreshLayout refreshView;
    @BindView(R.id.penpal_list)
    RecyclerView penpalList;
    @BindView(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    private List<PenpalResponse.ResponseBean> list;
    private PenpalListAdapter adapter;
    private int pageIndex = 1;
    private int pageSize = 10;

    @Override
    protected int attachLayoutRes() {
        return R.layout.fragment_penpal_joined;
    }

    @Override
    protected void initViews() {
        list = new ArrayList<>();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        penpalList.setLayoutManager(linearLayoutManager);
        adapter = new PenpalListAdapter(getActivity(), list);
        penpalList.setAdapter(adapter);
    }

    @Override
    protected void initData() {
        setPresenter(new PenpalJoinedPresenter(getActivity()), this);
    }

    @Override
    protected void updateViews(boolean isRefresh) {
        refreshView.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageIndex = 1;
                presenter.getPenpalList(pageIndex, pageSize);
            }
        });
        refreshView.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageIndex++;
                presenter.getPenpalList(pageIndex, pageSize);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        pageIndex = 1;
        presenter.getPenpalList(pageIndex, pageSize);
    }

    @Override
    public void success(Object object, int tag) {
        PenpalResponse response = (PenpalResponse) object;
        if (response == null) {
            return;
        }
        if (pageIndex == 1) {
            list.clear();
        }
        if (refreshView.isLoading()) {
            refreshView.finishLoadmore();
        }
        if (refreshView.isRefreshing()) {
            refreshView.finishRefresh();
        }
        list.addAll(response.getResponse());
        adapter.notifyDataSetChanged();
        noDataLayout.setVisibility(list.size() == 0 ? View.VISIBLE : View.GONE);
        if (list.size() < pageSize) {
            refreshView.setLoadmoreFinished(true);
        }
    }

    @Override
    public void faild(Object object, int tag) {

    }
}
